﻿#region Imported Namespaces

using System.Drawing;
using System.Reflection;
using Microsoft.WindowsCE.Forms;

#endregion

namespace Christian.Helle.PocketNews.PocketPC.Controls
{
    public static class NotificationBalloon
    {
        private const string ICON = "Christian.Helle.PocketNews.PocketPC.Icon.ico";
        private static Notification bubble;
        private static bool bubbleVisible;

        public static void Show(string caption, string text)
        {
            if (bubble == null)
            {
                bubble = new Notification();
                bubble.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(ICON), 16, 16);
                bubble.InitialDuration = 10;
                bubble.Critical = false;
                bubble.BalloonChanged += Bubble_BalloonChanged;
            }

            bubble.Caption = caption;
            bubble.Text = string.Format(
                "<font color=\"#{0}\"><b>{1}</b></font>",
                ConvertToHex(SystemColors.Highlight),
                text);

            bubble.Visible = true;
            bubbleVisible = true;
        }

        private static void Bubble_BalloonChanged(object sender, BalloonChangedEventArgs e)
        {
            if (!bubbleVisible && bubble != null)
            {
                bubble.Dispose();
                bubble = null;
            }

            bubbleVisible = !bubbleVisible;
        }

        public static void Kill()
        {
            if (bubble != null)
            {
                bubble.Dispose();
                bubble = null;
            }
        }

        private static string ConvertToHex(Color color)
        {
            return string.Format("{0}{1}{2}",
                                 ConvertToHex(color.R),
                                 ConvertToHex(color.G),
                                 ConvertToHex(color.B));
        }

        private static string ConvertToHex(int value)
        {
            var result = (value/16);
            var remain = (value%16);

            string str;

            switch (result)
            {
                case 10:
                    str = "A";
                    break;
                case 11:
                    str = "B";
                    break;
                case 12:
                    str = "C";
                    break;
                case 13:
                    str = "D";
                    break;
                case 14:
                    str = "E";
                    break;
                case 15:
                    str = "F";
                    break;
                default:
                    str = result.ToString();
                    break;
            }

            switch (remain)
            {
                case 10:
                    str += "A";
                    break;
                case 11:
                    str += "B";
                    break;
                case 12:
                    str += "C";
                    break;
                case 13:
                    str += "D";
                    break;
                case 14:
                    str += "E";
                    break;
                case 15:
                    str += "F";
                    break;
                default:
                    str += result.ToString();
                    break;
            }

            return str;
        }
    }
}